package com.example.sunmiv1.utils.net;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.example.sunmiv1.entity.LoginBean;
import com.example.sunmiv1.utils.LogUtil;
import com.github.houbb.opencc4j.util.ZhConverterUtil;
import com.github.houbb.opencc4j.util.ZhTwConverterUtil;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/8/17.
 */
public abstract class TextCallBack<T> implements Callback {

    public static final int CODE_SUCCESS = 1;
    public static final int CODE_FAILURE = 0;
    private static final String TAG = "TextCallBack";
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case CODE_FAILURE:
                    LogUtil.e("返回數據", "==========" + msg.obj);
                    onFailure((ResponseException) msg.obj);
                    break;
                case CODE_SUCCESS:
                    // 將簡體轉爲繁體
                    String result = (String) msg.obj;
                    Map map = GsonUtils.JsonToBean(result, Map.class);
                    String traditional = ZhConverterUtil.toTraditional(new Gson().toJson(map));
                    LogUtil.e("返回數據（簡體）", "==========" + result);
                    LogUtil.e("返回數據（繁體）", "==========" + traditional);
                    onSuccess(traditional);
                    break;
                default:
                    break;
            }
        }
    };

    /**
     * 主線程調用
     *
     * @param text
     */
    protected abstract void onSuccess(String text);

    /**
     * 主線程調用
     *
     * @param e
     */
    protected abstract void onFailure(ResponseException e);

    /**
     * 子線程調用
     *
     * @param call
     * @param e
     */
    @Override
    public void onFailure(Call call, IOException e) {
        ResponseException exception = null;
        if (e instanceof ResponseException == false) {
            exception = new ResponseException(e);
        } else {
            exception = (ResponseException) e;
        }

        Message message = mHandler.obtainMessage();
        message.what = CODE_FAILURE;
        message.obj = exception;
        mHandler.sendMessage(message);
    }

    /**
     * 子線程調用
     *
     * @param call
     * @param response
     * @throws IOException
     */
    @Override
    public void onResponse(Call call, Response response) throws IOException {
        if (response.code() == 200) {
            //成功
            String text = response.body().string();
            Message message = mHandler.obtainMessage();
            message.what = CODE_SUCCESS;
            message.obj = text;
            mHandler.sendMessage(message);
        } else { //成功
            String text = response.body().string();
            Log.e(TAG, "onResponse: 返回錯誤=" + text);
            onFailure(call, new ResponseException(response.code()));
        }
    }

    public static class ResponseException extends IOException {
        int code = -1;

        public ResponseException(IOException e) {
            super(e);
        }

        ResponseException(int code) {
            super("返回錯誤");
            code = code;
        }
    }

}
